﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReporterControlLibrary.Models;
using System.ComponentModel;
using System.Threading;
using Reporter.Data;

namespace Reporter.Controls
{
    /// <summary>
    /// Interaction logic for ProjectTplListBoxItem.xaml
    /// </summary>
    public partial class ProjectTplListBoxItem : UserControl, INotifyPropertyChanged
    {
        public ReportInfo ReportInfo
        {
            get { return this.DataContext as ReportInfo; }
        }

        //private bool _isGenerating = false;

        //public bool IsGenerating
        //{
        //    get { return _isGenerating; }
        //    set
        //    {
        //        if (this._isGenerating != value)
        //        {
        //            this._isGenerating = value;
        //            this.NotifyPropertyChanged("IsGenerating");
        //        }
        //    }
        //}

        //private int _reportedPercents = -1;

        //public int ReportedPercents
        //{
        //    get { return _reportedPercents; }
        //    set
        //    {
        //        if (this._reportedPercents != value)
        //        {
        //            this._reportedPercents = value;
        //            this.NotifyPropertyChanged("ReportedPercents");
        //        }
        //    }
        //}

        public ProjectTplListBoxItem()
        {
            InitializeComponent();

            generateButton.Click += new RoutedEventHandler(generateButton_Click);
        }

        void generateButton_Click(object sender, RoutedEventArgs e)
        {
            reportLogToggleButton.IsChecked = true;
        }

        //private void GenerateButtonClick(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    if (ReportInfo != null)
        //        ReportInfo.IsReporting = true;


        //    var tempReportID = ReportInfo.ReportTemplate.ReportTemplateID;

        //    ThreadPool.QueueUserWorkItem(new WaitCallback(
        //                                     (object o) =>
        //                                         {

        //                                             var reportid = tempReportID;

        //                                             for (int i = 0; i <= 100; i++)
        //                                             {
        //                                                 ProgressText.Dispatcher.Invoke(new Action(
        //                                                                                    () =>
        //                                                                                        {
        //                                                                                            var localReportInfo = AppData.GetReportInfoByID(reportid);

        //                                                                                            if (
        //                                                                                                localReportInfo != null)
        //                                                                                                localReportInfo.FinishedPercentage = i;
        //                                                                                        }
        //                                                                                    ), null);

        //                                                 Thread.Sleep(100);
        //                                             }

        //                                             //end of counting
        //                                             ProgressText.Dispatcher.Invoke(new Action(
        //                                                                                () =>
        //                                                                                    {
        //                                                                                        var localReportInfo = AppData.GetReportInfoByID(reportid);

        //                                                                                        if (localReportInfo != null)
        //                                                                                        {
        //                                                                                            localReportInfo.IsReporting = false;
        //                                                                                            localReportInfo.FinishedPercentage = -1;
        //                                                                                            localReportInfo.
        //                                                                                                LastReportDate =
        //                                                                                                DateTime.Now;
        //                                                                                        }
        //                                                                                    }
        //                                                                                ), null);
        //                                         }
        //                                     ));
        //}

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private void TemplateInfoChanged(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                UpdateSource(sender);
            }
        }

        private void UpdateSource(object sender)
        {
            var tb = sender as TextBox;

            if (tb == null) return;

            BindingExpression exp = tb.GetBindingExpression(TextBox.TextProperty);
            exp.UpdateSource();
        }

        private void  TemplateLostFocus(object sender, RoutedEventArgs e)
        {
            UpdateSource(sender);
        }
    }
}
